package com.kehutong.applet.service.impl;

import com.kehutong.applet.entity.AdvertEntity;
import com.kehutong.applet.entity.AppletWhiteEntity;
import com.kehutong.applet.service.AppletWhiteService;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindPage;

import java.util.List;

/**
 * @program: kht
 * @description: 小程序白名单实现
 * @author: joneron
 * @create: 2021/5/21
 **/
public class AppletWhiteServiceImpl implements AppletWhiteService {

    private static final Logger logger = LoggerFactory.getLogger(AppletWhiteServiceImpl.class);

    @Inject
    private JdbcSession jdbcSession;

    @Override
    public Page<AppletWhiteEntity> findPage(JSONObject jsonObject) {
        Page<AppletWhiteEntity> result = null;
        FindPage<AppletWhiteEntity> find = jdbcSession.findPage(AppletWhiteEntity.class)
                .like(jsonObject,"appletName")
                .order("createTime", "desc");

        find.page(jsonObject);

        return find.exe();
    }

    @Override
    public List<AppletWhiteEntity> findList(JSONObject jsonObject) {

        List<AppletWhiteEntity> appletList = jdbcSession.findArray(AppletWhiteEntity.class)
                .eq("deleted", false)
                .exe();

        return appletList;
    }

    @Override
    public Integer save(AppletWhiteEntity appletWhiteEntity) {
        return jdbcSession.insert(appletWhiteEntity);
    }

    @Override
    public Integer update(AppletWhiteEntity appletWhiteEntity) {
        return jdbcSession.updateById(appletWhiteEntity);
    }

    @Override
    public Integer deleteById(String id) {
        AppletWhiteEntity result = Pool.get(AppletWhiteEntity.class, id);
        if (result != null) {
            result.setDeleted(true);
            jdbcSession.updateById(result);
        }
        return 1;
    }
}
